//
//  MCMineViewController.m
//  MicroChat
//
//  Created by Mac on 2018/5/28.
//  Copyright © 2018年 stoneobs.qq.com. All rights reserved.
//

#import "MCMineViewController.h"
#import "MCSettingViewController.h"
#import "MCPersonalViewController.h"
#import "MCMineInfoViewController.h"
@interface MCMineViewController ()
@property(nonatomic, strong) NSArray                     *dataSouce;/**< 数据源 */

@property(nonatomic, strong) STButton                     *iconButton;/**< 头像 */
@property(nonatomic, strong) STButton                     *nickButton;/**< 昵称 */
@property(nonatomic, strong) STButton                     *idButton;/**< ID */

@property(nonatomic, strong) MCUserModel                     *userModel;/**< 用户信息 */
@end

@implementation MCMineViewController

- (void)setUserModel:(MCUserModel *)userModel{
    _userModel = userModel;
    [self.iconButton sd_setBackgroundImageWithURL:MCRealImageUrl(userModel.user_icon) forState:UIControlStateNormal];
    [self.nickButton setTitle:userModel.user_nick forState:UIControlStateNormal];
    [self.idButton setTitle:[NSString stringWithFormat:@"轻聊号:%@",
                             userModel.user_name] forState:UIControlStateNormal];
}
#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我";
    [self configSubView];
    [self configDataSouce];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     MCUserModel * user = [STUserManger defult].loginedUser;
    [TMUtils fecthUserModelWithUserId:user.userID.description handle:^(NSArray<MCUserModel *> *users) {
        self.userModel = users.firstObject;
        [[STUserManger defult] updateUserModel:users.firstObject];
    }];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 150)];
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 150)];
    imageView.image = [UIImage imageNamed:@"mine_bg"];
    [header addSubview:imageView];
    
    MCUserModel * user = [STUserManger defult].loginedUser;
    
    __weak typeof(self) weakSelf =  self;
    STButton * iconButton = [[STButton alloc] initWithFrame:CGRectMake(15, 0, 60, 60)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:30
                                           backgroundColor:UIColor.grayColor
                                           backgroundImage:[UIImage imageNamed:@"reba1"]
                                                     image:nil];
    iconButton.centerY = imageView.height/2;
    [iconButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedIconButton];
    }];
    [header addSubview:iconButton];
    [iconButton sd_setBackgroundImageWithURL:MCRealImageUrl(user.user_icon) forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"reba1"]];
    self.iconButton = iconButton;
    
    STButton * nickButton = [[STButton alloc] initWithFrame:CGRectMake(iconButton.right + 10, iconButton.top + 5 , 200, 20)
                                                     title:user.user_name
                                                titleColor:UIColor.whiteColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    [header addSubview:nickButton];
    self.nickButton = nickButton;
    
    STButton * idButton = [[STButton alloc] initWithFrame:CGRectMake(iconButton.right + 10, nickButton.bottom + 5, 200, 20)
                                                    title:[NSString stringWithFormat:@"轻聊号:%@",
                                                           user.user_name]
                                                 titleColor:UIColor.whiteColor
                                                  titleFont:15
                                               cornerRadius:0
                                            backgroundColor:nil
                                            backgroundImage:nil
                                                      image:nil];
    [header addSubview:idButton];
    self.idButton = idButton;
    self.tableView.tableHeaderView = header;
    
}
- (void)configDataSouce{
    
    NSArray * section0 = @[
                           [[STTableEasyModel alloc] initWithTextString:@"加入的群" imageName:@"qunzu"],
                           [[STTableEasyModel alloc] initWithTextString:@"官方公告" imageName:@"notice1"],
                           [[STTableEasyModel alloc] initWithTextString:@"官方客服" imageName:@"kefu"],
                           [[STTableEasyModel alloc] initWithTextString:@"关于我们" imageName:@"qunzu"],
                           ];
    
//    NSArray * section1 = @[
//                       [[STTableEasyModel alloc] initWithTextString:@"夺宝记录" imageName:@"夺宝2"],
//                       [[STTableEasyModel alloc] initWithTextString:@"我的钱包" imageName:@"我的钱包1"]
//                       ];
    
    NSArray * section2 = @[
                           [[STTableEasyModel alloc] initWithTextString:@"设置" imageName:@"setting拷贝"]
                           ];
    
    self.dataSouce = @[section0,section2];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.dataSouce[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    STTableEasyModel * mdoel = self.dataSouce[indexPath.section][indexPath.row];
    [cell setSt_tableEasyModel:mdoel];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    if ([model.textString isEqualToString:@"设置"]) {
        [self.navigationController pushViewController:[MCSettingViewController new] animated:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelctedIconButton{
    MCMineInfoViewController * vc = [MCMineInfoViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
